﻿using System;
using System.Linq;
using System.Linq.Expressions;
using SBACN.WebSite.Cache;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace SBACN.WebSite.Admin
{
	public partial class ContentConfig : AdminBasePage
	{
		/// <summary>
		/// 系统字典类型
		/// </summary>
		public string SystemType
		{
			get
			{
				//if (ViewState["SystemType"] == null)
				//    return "0001";//默认值
				//else
				//    return ViewState["SystemType"].ToString();
				return this.SystemCode;
			}
			private set
			{
				ViewState["SystemType"] = this.SystemCode;
			}
		}

		public string Code
		{
			get
			{
				if (ViewState["Code"] == null)
					return string.Empty;
				else
					return ViewState["Code"].ToString();
			}
			private set
			{
				ViewState["Code"] = value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (!string.IsNullOrWhiteSpace(Request["st"]))
				{
					this.SystemType = Request["st"].Trim();
				}
				if (!string.IsNullOrWhiteSpace(Request["c"]))
				{
					this.Code = Request["c"].Trim();
				}
				ShowList();
			}
		}

		protected void ShowList()
		{
			var sysMap = SystemMapCache.Instance[SystemType];
			if (sysMap != null)
			{
				var entity = SystemConfigCache.Instance[SystemType].Where(p => p.Type == this.SystemType).ToList();
				this.Repeater1.DataSource = entity;
				this.Repeater1.DataBind();
			}
		}

		protected void Sub_btn_Click(object sender, EventArgs e)
		{// 保存
			foreach (RepeaterItem item in Repeater1.Items)
			{
				var CodeID = item.FindControl("CodeID") as HiddenField;
				var ConfingMsg = item.FindControl("ConfingMsg") as TextBox;
				var ConfingSet = item.FindControl("ConfingSet") as TextBox;
				if (string.IsNullOrWhiteSpace(ConfingMsg.Text))
				{
					var Msg = item.FindControl("Msg") as Label;
					Msg.Text = "请填写标题。";
					continue;
				}
				else
				{
					var Msg = item.FindControl("Msg") as Label;
					Msg.Text = "";
				}
				if (string.IsNullOrWhiteSpace(ConfingSet.Text))
				{
					var Msg = item.FindControl("SetM") as Label;
					Msg.Text = "请填写内容。";
					continue;
				}
				else
				{
					var Msg = item.FindControl("SetM") as Label;

					if (this.SystemType == "00090005" || this.SystemType == "00090006")
					{
						int Count = 0;
						if (!int.TryParse(ConfingSet.Text, out Count))
						{
							Msg.Text = "请填写数字！";
							continue;
						}
					}

					Msg.Text = "";
				}
				var Status = item.FindControl("status") as CheckBox;
				Expression<Func<TB_SystemConfig, bool>> fun = (p => p.Code == CodeID.Value && p.Type == this.SystemType);
				var result = DAL.DALHelper.Instance.GetFirstOrDefaultObject<TB_SystemConfig>(fun);
				if (result != null)
				{
					result.Name = ConfingMsg.Text;
					result.Value = ConfingSet.Text;
					if (Status.Checked)
					{
						result.Status = true;
					}
					else
					{
						result.Status = false;
					}

					DAL.DALHelper.Instance.Update<TB_SystemConfig>(result);
				}
			}

			DAL.DALHelper.Instance.SaveChanges();
			SystemConfigCache.Instance.Clear();
		}
	}
}